package mk.gamemap.view.sprite
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	
	import game.tileMap.MapConst;
	import game.tileMap.SpriteCache;
	import game.tileMap.model.vo.MapObj;
	
	import mk.gamemap.view.anime.IAnime;

	public class SpriteView extends EventDispatcher
	{
	//	public var X:int=0;
	//	public var Y:int=0;
		public var sp:Sprite
		public var curPicId:String;
		public var vo:MapObj
		protected var animation:IAnime;
		protected var spCache:SpriteCache;
		public function SpriteView(model:MapObj)
		{
			vo=model;
			init()
		}
		
		
		public function get X():int{
			return vo.x;
		}
		
		public function get Y():int{
			return vo.y;
		}
		
		public function set X(x:int):void{
			 vo.x=x;
		}
		
		public function set Y(y:int):void{
			vo.y=y;
		}
		
		public function init():void{
			spCache=new SpriteCache();
			
			curPicId=vo.picId;
			sp=spCache.creatSpView(curPicId);
		}
		
		
		
		public function updatePic():void{
			if (curPicId!=vo.picId){
				if (sp.parent){
					var canvas:DisplayObjectContainer=sp.parent as DisplayObjectContainer;
					sp.parent.removeChild(sp);
					curPicId=vo.picId;
					sp=spCache.creatSpView(curPicId);
					if (sp){
						sp.x=X*MapConst.TileSize;
						sp.y=Y*MapConst.TileSize;
						canvas.addChild(sp);
					}
					
				}
			}
		}
		
		
		public function update():void{
			updatePic();
			if (sp){
				sp.x=vo.x*MapConst.TileSize;
				sp.y=vo.y*MapConst.TileSize;
			}
			
				if (animation){
					animation.nextFrame();
				}
		}
	}
}